#!/usr/bin/python
# Copyright (c) 2012 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Replace Tools
"""

def ReplaceMap(arch):
  ARCH_MAP = {
    'arm': 'arm',
    'i686': 'i686',
    'x86_64': 'amd64'
  }
  CPU_MAP = {
    'arm': 'arm',
    'i686': 'x86',
    'x86_64': 'x86',
  }
  GCC_MAP = {
    'arm': 'arm',
    'i686': 'i686',
    'x86_64': 'x86_64'
  }
  INC_MAP = {
    'arm' : 'arm-nacl/include',
    'i686': 'x86_64-nacl/include',
    'x86_64': 'x86_64-nacl/include',
  }
  LIB_MAP = {
    'arm' : 'arm-nacl/lib',
    'i686': 'x86_64-nacl/lib32',
    'x86_64': 'x86_64-nacl/lib',
  }
  MATH_MAP = {
    'arm': 'arm',
    'i686': 'i386',
    'x86_64': 'amd64'
  }
  NACL_MAP = {
    'arm': 'arm-nacl',
    'i686': 'x86_64-nacl',
    'x86_64': 'x86_64-nacl',
  }
  MACH_MAP = {
    'arm': 'arm',
    'i686': 'x86',
    'x86_64': 'amd64'
  }
  SCON_MAP = {
    'arm': 'arm',
    'i686': 'x86-32',
    'x86_64': 'x86-64'
  }

  REPLACE_MAP = {
    '$ARCH': ARCH_MAP[arch],
    '$CPU': CPU_MAP[arch],
    '$GCC': GCC_MAP[arch],
    '$INC': INC_MAP[arch],
    '$LIB': LIB_MAP[arch],
    '$MATH': MATH_MAP[arch],
    '$MACH': MACH_MAP[arch],
    '$NACL': NACL_MAP[arch],
    '$SCON': SCON_MAP[arch],
  }
  return REPLACE_MAP


def ReplaceText(text, maplist):
  for m in maplist:
    for key in m:
      text = text.replace(key, m[key])
  return text


def ReplaceArch(text, arch):
  return ReplaceText(text, [ReplaceMap(arch)])


def ReplaceList(items, arch):
  replace = ReplaceMap(arch)
  return [ReplaceText(item, [replace]) for item in items]
  